/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.ruralLibrary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.entity.SysRole;
import com.pig4cloud.pigx.admin.api.feign.RemoteDataScopeService;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.ruralLibrary.api.entity.*;
import com.pig4cloud.pigx.ruralLibrary.mapper.FeedBackMapper;
import com.pig4cloud.pigx.ruralLibrary.service.BookUserService;
import com.pig4cloud.pigx.ruralLibrary.service.FeedBackService;
import lombok.extern.slf4j.Slf4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 资源
 *
 * @author pig
 * @date 2023-10-31 14:34:33
 */
@Service
@Slf4j
public class FeedBackServiceImpl extends ServiceImpl<FeedBackMapper, FeedBack> implements FeedBackService {
	private final Logger logger = LoggerFactory.getLogger(FeedBackServiceImpl.class);

	@Autowired(required = false)
	private RemoteDataScopeService remoteDataScopeService;
	@Autowired
	private BookUserService bookUserService;

    @Override
    public R pageList(Page page, LambdaQueryWrapper<FeedBack> wrapper) {
		logger.info("=======当前登录用户的角色信息={}",SecurityUtils.getRoles());
		R<List<SysRole>> listR=remoteDataScopeService.getRoleList(SecurityUtils.getRoles().stream()
				.map(String::valueOf)
				.collect(Collectors.toList()));
		if (listR != null) {
			if ("PROVINCIAL_ROLE".equals(listR.getData().get(0).getRoleCode())) {
				wrapper.eq(FeedBack::getProvince, "41");
			}
			if ("MUNICIPAL_ROLE".equals(listR.getData().get(0).getRoleCode())) {
				BookUserEntity bookUser = bookUserService.getById(SecurityUtils.getUser().getId());
				if (bookUser != null) {
					wrapper.eq(FeedBack::getCity, bookUser.getCity());
				}
			}
			if ("DISTRICT_ROLE".equals(listR.getData().get(0).getRoleCode())) {
				BookUserEntity bookUser = bookUserService.getById(SecurityUtils.getUser().getId());
				if (bookUser != null) {
					wrapper.eq(FeedBack::getArea, bookUser.getDistrict());
				}
			}
			if ("VILLAGE_ROLE".equals(listR.getData().get(0).getRoleCode())) {
				BookUserEntity bookUser = bookUserService.getById(SecurityUtils.getUser().getId());
				if (bookUser != null) {
					wrapper.eq(FeedBack::getStreet, bookUser.getVillage());
				}
			}
			if ("COMMITTEE_ROLE".equals(listR.getData().get(0).getRoleCode())) {
				BookUserEntity bookUser = bookUserService.getById(SecurityUtils.getUser().getId());
				if (bookUser != null) {
					wrapper.eq(FeedBack::getCommittee, bookUser.getCommittee());
				}
			}
		}
		wrapper.orderByDesc(FeedBack::getCreatedTime);
        Page<FeedBack> paged = this.page(page, wrapper);
        return R.ok(paged);
    }

}
